package ConveyorBelt.State;

import java.io.IOException;
import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import ConveyorBelt.Util.FontUtil;
import ConveyorBelt.Util.ImageUtil;
import ConveyorBelt.Util.ScoreRecord;
import ConveyorBelt.Util.ScoreTableLoader;

public class ScoreTable extends BasicGameState {
	public static final int stateID = 3;
	private int gameDialogX, gameDialogY;
	private int	exitGameButtonX, exitGameButtonY;
	private int backButtonX, backButtonY;
	private int mouseX, mouseY;
    private FadeOutTransition fot;
    private FadeInTransition fit;
    private float exitGameScale = 1;
	private float backScale = 1;
	private float scaleStep = 0.0001f;
	private ScoreTableLoader loader;
	private ArrayList<ScoreRecord> sr;
	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		ImageUtil.loadAllImage();
		FontUtil.loadGameFont();
    	gameDialogX = gc.getWidth()/2 - ImageUtil.gameDialog.get(2).getWidth()/2;
		gameDialogY = gc.getHeight()/2 - ImageUtil.gameDialog.get(2).getHeight()/2 - 50;		
		exitGameButtonX = gameDialogX + ImageUtil.gameDialog.get(2).getWidth() - ImageUtil.exitGameButton.getWidth() + 100;
		exitGameButtonY = gameDialogY + ImageUtil.gameDialog.get(2).getHeight() + 25;
		backButtonX = gameDialogX - 100;
		backButtonY = gameDialogY + ImageUtil.gameDialog.get(2).getHeight() + 25;
		try {
			loader = new ScoreTableLoader("resources/score/scores.properties");
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		sr = new ArrayList<ScoreRecord>();
		sr = loader.loadScoreTable(loader.filePath);
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		ImageUtil.background.draw(0, 0);
		ImageUtil.gameDialog.get(2).draw(gameDialogX, gameDialogY);
		ImageUtil.exitGameButton.draw(exitGameButtonX, exitGameButtonY);
		ImageUtil.backButton.draw(backButtonX, backButtonY);
		g.setFont(FontUtil.uf);
 		g.drawString("OOP 2D Game Project:      Can You Help Me???      - Author: Phu - Manh - Quang", 225, 650);
 		FontUtil.changeColor(java.awt.Color.GREEN);
 		for(int i=0; i<10; i++) {
 			g.drawString(i+1 + ".", 500, 160 + i*35);
 			g.drawString(sr.get(i).getName(), 550, 160 + i*35);
 			g.drawString(""+sr.get(i).getScore(), 700, 160 + i*35);
 		}
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		init(gc, sbg);
		Input input = gc.getInput();
		mouseX = input.getMouseX();
		mouseY = input.getMouseY();
		boolean insideExitGame = false;
		boolean insideBack = false;
		int tmpX;
		int tmpY;
		fot = new FadeOutTransition(Color.black);
    	fit = new FadeInTransition(Color.black);
		if((mouseX >= exitGameButtonX && mouseX <= exitGameButtonX + ImageUtil.exitGameButton.getWidth()) && 
				(mouseY >= exitGameButtonY && mouseY <= exitGameButtonY + ImageUtil.exitGameButton.getHeight())) {
			insideExitGame = true;
		}
		tmpY = gameDialogY + ImageUtil.gameDialog.get(1).getHeight() + 25;
		if(insideExitGame) {
			tmpX = gameDialogX + ImageUtil.gameDialog.get(1).getWidth() - ImageUtil.exitGameButton.getWidth() + 100 + 10;
			if(exitGameScale < 1.05f) exitGameScale += scaleStep*delta;
			if(exitGameButtonX < tmpX) exitGameButtonX ++;
			if(exitGameButtonY >= tmpY) exitGameButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) gc.exit();
		}
		else {
			tmpX = gameDialogX + ImageUtil.gameDialog.get(1).getWidth() - ImageUtil.exitGameButton.getWidth() + 100;
			if(exitGameScale > 1.0f) exitGameScale -= scaleStep*delta;
			if(exitGameButtonX > tmpX) exitGameButtonX--;
			if(exitGameButtonY <= tmpY) exitGameButtonY++;
		}
		
		if((mouseX >= backButtonX && mouseX <= backButtonX + ImageUtil.backButton.getWidth()) && 
				(mouseY >= backButtonY && mouseY <= backButtonY + ImageUtil.backButton.getHeight())) {
			insideBack = true;
		}
		tmpY = gameDialogY + ImageUtil.gameDialog.get(1).getHeight() + 25;
		if(insideBack) {
			tmpX = gameDialogX - 100 + 10;
			if(backScale < 1.05f) backScale += scaleStep*delta;
			if(backButtonX < tmpX) backButtonX ++;
			if(backButtonY >= tmpY) backButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) sbg.enterState(MainMenu.stateID, fot, fit);
		}
		else {
			tmpX = gameDialogX - 100;
			if(backScale > 1.0f) backScale -= scaleStep*delta;
			if(backButtonX > tmpX) backButtonX--;
			if(backButtonY <= tmpY) backButtonY++;
		}
	}

	@Override
	public int getID() {
		return stateID;
	}
}
